﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TextGlow.Control.Packaging;
using System.IO;
using System.Windows.Resources;

namespace TextGlow.Control
{
  public class PackageContentGetter : IContentGetter
  {
    private Stream _stream;
    private StreamResourceInfo _zipResourceInfo;

    public PackageContentGetter(Stream s)
    {
      if (s == null)
        throw new ArgumentNullException("s");

      _stream = s;
      _zipResourceInfo = new StreamResourceInfo(_stream, null);
    }

    public Stream GetContent(string partName)
    {
      StreamResourceInfo contentInfo = Application.GetResourceStream(_zipResourceInfo, new Uri(partName, UriKind.Relative));

      return contentInfo.Stream;
    }

    public string GetStringContent(string partName)
    {
      Stream content = GetContent(partName);

      if (content.Length == 0)
      {
        return null;
      }
      else
      {
        using (content)
        using (StreamReader sr = new StreamReader(content))
        {
          return sr.ReadToEnd();
        }
      }

    }
  }
}